#ifndef _MYFTPSERVER_H 
#define _MYFTPSERVER_H

#define LINEEND "\r\n"
#define LISTENQ 5
#define MAXSIZE 2560
#define MAXPATH 64
#define USERNAMEMAX 32
#define MAXFD 64
#define FLAGS S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
#include "common.h"

int listenfd;
int confd;
struct sockaddr_in cliaddr, servaddr;
char buf[MAXSIZE];
extern char *optarg;
extern int errno;

char serviceReply[][256] = {
	"150 File status ok;about to open data connection, port %d" LINEEND,
	"220 Server ready for new user" LINEEND,
	"200 Command ok" LINEEND,
	"226 Closing data connection" LINEEND,
	"230 User logged in, proceed" LINEEND, 
	"331 User name ok, need password" LINEEND,
	"550 Requested action not taken:%s" LINEEND,
	"530 Not logged in" LINEEND,
	"500 Syntax error, command unrecognized" LINEEND,
	"230 User logged in, proceed" LINEEND,
	"530 Not logged in" LINEEND,
	"331 User name okay, need password" LINEEND,
	"332 Need account for login" LINEEND,
	"227 Entering Passive Mode (%d, %d, %d, %d, %d, %d)" LINEEND,
	"221 Service closing control connection" LINEEND,
	"125 Data connection already open; transfer starting" LINEEND,
	"150 File status okay; about to open data connection" LINEEND,
	"226 Closing data connection" LINEEND,
	"250 Requested file action okay, completed" LINEEND,
	"215 UNIX Type: L8" LINEEND,
	"425 Can't open data connection" LINEEND,
	"257 %s created" LINEEND,
	"450 Request file action not taken:%s" LINEEND,
	"501 Username invalid" LINEEND,
	"503 Password invalid" LINEEND
};

typedef struct userpasswd{
	char *username;
	char *passwd;
}usertype;

usertype users[] = {
	{"USER ftp", "PASS 123"},
	{"USER anonymouse", "PASS"}
}; 

typedef struct cmd_operation {
	char *cmd;
	int (*operation)(int confd, char *cmd);
} cmdOperation;

int do_cmd_user(int fd, char *cmdline);
int do_cmd_pass(int fd, char *cmdline);
int do_cmd_syst(int fd, char *cmdline);
int do_cmd_pwd(int fd, char *cmdline);
int do_cmd_cwd(int fd, char *cmdline);
int do_cmd_delete(int fd, char *cmdline);
int get_list(const char *path, char *buf, int size);
int get_con_fd();
int do_cmd_list(int fd, char *cmdline);
int do_cmd_quit(int fd, char *cmdline);
int do_cmd_pasv(int fd, char *cmdline);
int do_cmd_mkd(int fd, char *cmdline);
int do_cmd_cdup(int fd, char *cmdline);
int myrmdir(const char *dir); 
int do_cmd_rmd(int fd, char *cmdline);
int do_cmd_port(int fd, char *cmdline);
int do_cmd_noop(int fd, char *cmdline);
int do_cmd_stor(int fd, char *cmdline);
int do_cmd_retr(int fd, char *cmdline);
int do_cmd_rnfr(int fd, char *cmdline);
int do_cmd_rnto(int fd, char *cmdline);
int do_cmd_type(int fd, char *cmdline); 

cmdOperation commands[] = {
	{"USER", do_cmd_user},
	{"PASS", do_cmd_pass},
	{"SYST", do_cmd_syst},
	{"PWD", do_cmd_pwd},
	{"TYPE", do_cmd_type},
	{"CWD", do_cmd_cwd},
	{"DELE", do_cmd_delete},
	{"LIST", do_cmd_list},
	{"QUIT", do_cmd_quit},
	{"PASV", do_cmd_pasv},
	{"MKD", do_cmd_mkd},
	{"CDUP", do_cmd_cdup},
	{"RMD", do_cmd_rmd},
	{"PORT", do_cmd_port},
	{"NOOP", do_cmd_noop},
	{"STOR", do_cmd_stor},
	{"RETR", do_cmd_retr},
	{"RNFR", do_cmd_rnfr},
	{"RNTO", do_cmd_rnto}
};

#endif
